#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>

#include "util/delay_x.h"
#include "uart/uart.h"
#include "adc/adc.h"

#define sbi(port,pin)    port |= 1<<pin    //set bit
#define cbi(port,pin)    port &= ~(1<<pin) //clear bit
#define UART_BAUD_RATE 115200
#define STREAM_BUFFER_SIZE 32

int main(void)
{
	PORTA = 0; //set port a as input.
	uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
	sei();

	ADC_init();
	ADC_startScan();

	DDRB = 0b11111111;
	PORTB = 0b11111111;

	char data_stream_buffer[STREAM_BUFFER_SIZE];
	memset(data_stream_buffer, '\0', STREAM_BUFFER_SIZE);
	char seperator[1];
	seperator[0]=',';
	char end_of_transmission[1];
	end_of_transmission[0]='\n';

	char tmp_val[5];

	while(1)
	{
		//result
		for (int i = 0; i < ADC_CHANNELS; ++i)
		{
			itoa(ADC_raw_values[i], tmp_val, 10);

			if(i < ADC_CHANNELS-1)
			{
				strncat(data_stream_buffer, tmp_val, 4);
				strncat(data_stream_buffer, seperator, 1);
			}
			else
			{
				strncat(data_stream_buffer, tmp_val, 4);
			}
		}
		strncat(data_stream_buffer,end_of_transmission,1);
		uart_puts(data_stream_buffer);
		memset(data_stream_buffer, '\0', STREAM_BUFFER_SIZE);
	}
}
